<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline>
          <el-form-item>
            <el-button
              icon="el-icon-plus"
              type="success"
              @click="deBounceWrap(handleAdd)"
            >新增
            </el-button
            >
            <!-- <el-button type="danger" icon="el-icon-delete">删除</el-button> -->
          </el-form-item>
        </el-form>
        <div class="flex mt-3">
          <el-select
            v-model="listQuery.category_id"
            clearable
            placeholder="请选择礼物类别"
            style="width: 17%"
          >
            <el-option
              v-for="item in styleLabelOptions"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              clearable
            >
            </el-option>
          </el-select>

          <el-input
            v-model="listQuery.intimacy"
            class="ml-4"
            clearable
            placeholder="请输入亲密值要求"
            style="width: 17%"
          ></el-input>
          <el-input
            v-model="listQuery.price"
            class="ml-4"
            clearable
            placeholder="请输入价格"
            style="width: 17%"
          ></el-input>
        </div>
        <div class="flex mt-3">
          <el-select
            v-model="listQuery.sale_status"
            clearable
            placeholder="请选择出售状态"
            style="width: 17%"
          >
            <el-option label="上架" value="1"></el-option>
            <el-option label="下架" value="0"></el-option>
          </el-select>
          <el-date-picker
            v-model="listQuery.add_time"
            class="ml-4"
            clearable
            format="yyyy - MM - dd "
            placeholder="请选择添加时间"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
          <el-input
            v-model="listQuery.product_name"
            class="ml-4"
            clearable
            placeholder="请输入礼物名称"
            style="width: 17%"
          ></el-input>

          <div class="ml-4 flex items-center" style="width: 17%">
            <i
              class="el-icon-search p-1 text-white text-2xl bg-green-500 cursor-pointer"
              @click="getList"
            ></i>
            <i
              class="el-icon-refresh p-1 ml-3 text-white text-2xl bg-red-500 cursor-pointer"
              @click="resetClick"
            ></i>
          </div>
        </div>
      </div>

      <el-table :data="data" border>
        <!-- <el-table-column type="selection" width="55"></el-table-column> -->
        <el-table-column label="序号" prop="id" width="150px"></el-table-column>
        <el-table-column
          label="礼物类别"
          prop="category_name"
        ></el-table-column>
        <el-table-column label="礼物名称" prop="product_name"></el-table-column>
        <el-table-column label="亲密值要求" prop="intimacy"></el-table-column>
        <el-table-column label="价格" prop="original_price"></el-table-column>
        <el-table-column label="天数/天" prop="day"></el-table-column>
        <el-table-column label="动态效果图" prop="special">
          <template slot-scope="scope">
            <div v-if="/.mp4$/.test(scope.row.special)">
              <el-popover max-width="220" placement="right" trigger="hover">
                <video
                  :src="scope.row.special"
                  controls
                  style="max-width: 200px"
                ></video>
                <video
                  slot="reference"
                  :src="scope.row.special"
                  autoplay
                  loop
                  muted
                  style="width: 50px"
                ></video>
              </el-popover>
            </div>
            <div v-else-if="/.webp$/.test(scope.row.special)">
              <el-popover max-width="220" placement="right" trigger="hover">
                <img
                  :src="scope.row.special"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.special"
                  alt=""
                  style="width: 50px"
                />
              </el-popover>
            </div>
            <div v-else>
              <p>{{ scope.row.special }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="展示图片" prop="picture">
          <template slot-scope="{ row }">
            <el-popover max-width="420" placement="right" trigger="hover">
              <img
                :src="row.picture"
                alt=""
                style="max-width: 200px; max-height: 200px"
              />
              <img
                slot="reference"
                :src="row.picture"
                alt=""
                style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="出售状态" prop="sold">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.on_sale === 1" type="success">上架</el-tag>
            <el-tag v-else type="danger">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="add_time"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              plain
              size="mini"
              type="primary"
              @click="deBounceWrap(handleEdit, scope.row)"
            >编辑
            </el-button
            >
            <!-- <el-button type="primary" plain size="mini" icon="el-icon-present">
              赠送
            </el-button> -->
            <!-- <el-button
              type="danger"
              plain
              size="mini"
              icon="el-icon-delete"
              @click="deBounceWrap(handleDel, scope.row)"
              >删除</el-button
            > --> </template
          >
          ``
        </el-table-column>
      </el-table>
      <cus-pagination
        :limit.sync="listQuery.page_size"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 删除弹框
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style: normal; color: red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog> -->

    <addEditModal
      :categoryOption="styleLabelOptions"
      :dialogVisible.sync="dialogVisible"
      :row="rowData"
      :status.sync="title"
      @updateTable="handleSearch"
    ></addEditModal>
  </div>
</template>

<script>


import addEditModal from './comps/addEditModel'

import { cupidAdd, cupidEdit, cupidPropList, giftType } from '@/api/IntimateHome/IntimateHome'

export default {
  components: {
    addEditModal
  },
  data() {
    return {

      specialList: [],
      data: [],
      styleLabelOptions: [],
      prizepoolAll: [],
      listQuery: {
        category_id: '',
        product_name: undefined,
        price: undefined,
        intimacy: undefined,
        sale_status: undefined,
        add_time: undefined,
        page: 1,
        page_size: 10
      },
      total: 0,
      titleOptions: {
        add: '新增',
        edit: '编辑'
      },
      title: 'add',
      rowData: {},
      form: {
        category_id: undefined,
        product_name: undefined,
        picture: undefined,
        special: undefined,
        day: undefined,
        price: undefined,
        intimacy: undefined,
        sale_status: undefined
      },

      rules: {
        day: [{ required: true, message: '请输入礼物名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入礼物价格', trigger: 'blur' }],
        intimacy: [
          { required: true, message: '请输入亲密值要求', trigger: 'blur' }
        ],
        special: [
          { required: true, message: '请选择上传动效', trigger: 'change' }
        ],
        sale_status: [
          { required: true, message: '请选择出售状态', trigger: 'change' }
        ],
        picture: [
          { required: true, message: '请选择上传图片', trigger: 'change' }
        ],
        category_id: [
          { required: true, message: '请选择礼物类别', trigger: 'change' }
        ],
        product_name: [
          { required: true, message: '请输入礼物名称', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    }
  },
  created() {
    this.getList()
    this.getGiftType()
  },
  methods: {
    //列表
    getList() {
      let data = JSON.parse(JSON.stringify(this.listQuery))
      if (!this.listQuery.sale_status) {
        data.sale_status = -1
      }
      cupidPropList(data).then(res => {
        this.data = res.data.data
        this.total = res.data.total
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    //获取礼物类型
    getGiftType() {
      giftType().then(res => {
        this.styleLabelOptions = res.data
      })
    },
    handleSuccess() {
      this.form.picture = this.ossLastImgUrl
    },
    handleBeforeUploadPicture(file) {
      const isPic =
        file.type === 'image/jpg' ||
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/webp'

      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPic) {
        this.$message.error('请上传图片!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPic && isLt2M
    },

    handleChangePic(fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    handleBeforeUploadSpecial(file) {
      const isMp4 = file.type === 'video/mp4' || file.type === 'image/webp'
      if (!isMp4) {
        this.$message.error('上传特效只能为 MP4 格式或WEBP格式!')
      }
      return isMp4
    },
    handleSpecialFlagBigRemove(res) {
      this.form.special = undefined
      this.specialList = []
    },
    handleSpecialFlagBigSuccess(res) {
      this.form.special = res.data.url
      this.specialList = [{ name: res.data.url }]
    },
    //重置
    resetClick() {
      this.listQuery = {
        category_id: null,
        product_name: undefined,
        price: undefined,
        intimacy: null,
        sale_status: null,
        add_time: undefined,
        page: 1,
        page_size: 10
      }

      this.getList()
    },
    //新增
    handleAdd() {
      this.title = 'add'
      this.dialogVisible = true
    },

    // 编辑
    handleEdit(row) {
      this.title = 'edit'
      this.rowData = row

      this.dialogVisible = true
    },
    handleSumbit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title === 'add') {
            let data = {
              category_id: this.form.category_id,
              product_name: this.form.product_name,
              picture: this.form.picture,
              special: this.form.special,
              sale_status: this.form.sale_status
            }
            let price_info = JSON.stringify({
              day: this.form.day,
              price: this.form.price,
              intimacy: this.form.intimacy
            })
            let data1 = { ...data, price_info }

            cupidAdd(data1).then(res => {
              if (res.code === 200) {
                this.$message.success('提交成功')
                this.getList()
                this.dialogVisible = false
              } else {
              }
            })
          }
          if (this.title === 'edit') {
            let data = {
              id: this.row.id,
              category_id: this.form.category_id,
              product_name: this.form.product_name,
              picture: this.form.picture,
              special: this.form.special,
              sale_status: this.form.sale_status
            }
            let price_info = JSON.stringify({
              day: this.form.day,
              price: this.form.price,
              intimacy: this.form.intimacy
            })
            let data1 = { ...data, price_info }
            cupidEdit(data1).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.getList()
                this.dialogVisible = false
              }
            })
          }
        } else {

          return false
        }
      })
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false
    },
    handleClear() {
      this.$refs['form'].resetFields();
      (this.form = {
        category_id: undefined,
        product_name: undefined,
        picture: undefined,
        special: undefined,
        day: undefined,
        price: undefined,
        intimacy: undefined,
        sale_status: undefined
      }),
        (this.specialList = [])
    }
    //删除
    // handleDel(row) {
    //   this.dataIds = { ids: `${row.id}` };
    //   this.deleteDialogVisible = true;
    // },
    // 确定删除
    // confirmDelete() {
    //   cupidDel(this.dataIds).then((response) => {
    //     if (response.code === 200) {
    //       this.getList();
    //       this.deleteDialogVisible = false;
    //       this.$notify({
    //         type: "success",
    //         message: "操作成功",
    //       });
    //     }
    //   });
    // },
  }
}
</script>
<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}

/deep/ .el-upload-list__item {
  transition : none !important;
}

/deep/ .el-upload-list--picture {
  max-height : 103px !important;
  overflow   : hidden;
}

/deep/ .el-upload-list--text {
  max-height : 100px !important;
  overflow   : hidden;
}

/deep/ .el-upload-list__item-name {
  white-space : normal !important;
}
</style>
